package com.yw.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.yw.databaseManageInfo.domain.DatabaseManageInfo;
import com.yw.databaseManageInfo.dto.DatabaseAddDTO;
import com.yw.databaseManageInfo.dto.DatabaseManageDTO;
import com.yw.databaseManageInfo.vo.DatabaseManageInfoVO;
import com.yw.result.PageDTO;
import com.yw.result.R;
import com.yw.service.DatabaseManageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@Tag(name = "数据库管理",description = "数据源管理")
@RequestMapping("/database")
public class DatabaseManageController {

    @Resource
    private DatabaseManageService databaseManageService;


    @Operation(summary = "查询单条数据源",description = "查询单条数据源")
    @PostMapping("/getOneDatabase/{id}")
    public R pageOne(@PathVariable Integer id){
        DatabaseManageInfo one = databaseManageService.getById(id);
        return R.OK(one);
    }

    @Operation(summary = "条件查询数据源",description = "条件查询数据源")
    @PostMapping("/pageDatabaseByQuery")
    public R pageAllByQuery(@RequestBody DatabaseManageDTO databaseManageDTO){
        Page<DatabaseManageInfoVO> page = databaseManageService.pageAllByQuery(databaseManageDTO);
        return R.OK(page);
    }

    @Operation(summary = "新增数据源",description = "新增数据源")
    @PostMapping("/addDatabase")
    public R addDatabase(@RequestBody DatabaseAddDTO databaseAddDTO){
        return databaseManageService.addDatabase(databaseAddDTO)?R.OK():R.FAIL();
    }

    @Operation(summary = "删除数据源",description = "删除数据源")
    @PostMapping("/deleteDatabase")
    public R deleteDatabase(@RequestParam Integer id){
        return databaseManageService.removeById(id)?R.OK():R.FAIL();
    }

    @Operation(summary = "更改数据源",description = "更改数据源")
    @PostMapping("/updateDatabase/{id}")
    public R updateDatabase(@PathVariable Integer id,@RequestBody DatabaseAddDTO databaseAddDTO){
        return databaseManageService.updateDatabase(id,databaseAddDTO)?R.OK():R.FAIL();
    }

    @Operation(summary = "更改数据源状态",description = "更改数据源状态")
    @PostMapping("/updateStatic/{id}")
    public R updateStatic(@PathVariable Integer id){
        return databaseManageService.updateStatic(id)?R.OK():R.FAIL();
    }

    @Operation(summary = "数据库联通测试",description = "数据库联通测试")
    @PostMapping("/checkConnection")
    public R  checkConnection(Integer id){
        return databaseManageService.checkConnection(id)?R.OK("联通成功"):R.FAIL("联通失败");
    }


}
